
<template>
  <div class="bodys">
    <!-- <div class="tan" @click="gotop" :v-model="gotops">
      <van-icon name="arrow-up" />顶部
    </div> -->
    <!-- 顶部搜索栏 -->
    <div class="header">
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="search" @click="$router.push({ path: '/mysearch' })">
        <!-- <div class="search"> -->
        <van-icon name="search" />
        <span class="sousuo">搜索新闻</span>
      </div>
      <div class="user" @click="jumppersonal">
        <van-icon name="manager-o" />
      </div>
    </div>
    <div class="both"></div>
    <!-- 标签栏 -->
    <div class="tab">
      <van-tabs
        class="tabs"
        swipeable
        sticky
        animated
        offset-top="45px"
        v-model="active"
        background="rgba(223, 223, 223)"
      >
        <van-tab v-for="value in arr" :title="value.name" :key="value.id">
          <van-list
            v-model="value.loading"
            :finished="value.finished"
            finished-text="这已经是我的底线了 ..."
            error-text="老子拼命加载却败给了网络..."
            @load="onLoad"
            :offset="10"
            :immediate-check="false"
          >
            <van-pull-refresh v-model="value.isLoading" @refresh="onRefresh">
              <art
                v-for="item in value.postlist"
                :key="item.id"
                :post="item"
              ></art> </van-pull-refresh
          ></van-list>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>


<script>
import { getlistname } from "@/apis/category";
import { postlists } from "@/apis/postlists";
import art from "@/components/articles";

export default {
  methods: {
    //会带顶部的绿条
    gotop() {
      setTimeout(() => {
        window.scrollTo(0, 0);
      }, 0);
      this.gotops = false;
    },
    //封装获取页面数据函数
    async init() {
      // 上传的数据
      let res = await postlists({
        category: this.arr[this.active].id,
        pageIndex: this.arr[this.active].pageIndex,
        pageSize: this.arr[this.active].pageSize,
      });

      // console.log(res);
      //将获取的数据存储到当前栏目的的postlist数组中
      // this.arr[this.active].postlist = res.data.data;

      //将获取的数据追加存储到postlist数组内
      this.arr[this.active].postlist.push(...res.data.data);
      // console.log(1);
      //本次加载完成将loading重置为false,以便下次上拉
      this.arr[this.active].loading = false;

      //如果没有新数据了,将finished重置为true
      if (res.data.data.length < this.arr[this.active].pageSize) {
        this.arr[this.active].finished = true;
        // console.log("没有数据了");
      }
      // console.log(4);
    },
    //加载续页数据
    onLoad() {
      // console.log("续页谢谢...");
      this.arr[this.active].isLoading = false;
      this.arr[this.active].pageIndex++;
      if (this.arr[this.active].pageIndex > 1) {
        this.gotops = true;
      }
      this.init();
    },
    //上拉获取新数据
    onRefresh() {
      // console.log("上拉了");
      this.arr[this.active].postlist.length = 0; // 清空数据
      this.arr[this.active].pageIndex = 1; // 重置页码到1
      // 不管之前是否还有数据，将可能被重置的finished重置为false
      this.arr[this.active].finished = false;

      this.init();
      this.onLoad();
      // this.arr[this.active].loading = false;
    },
    //个人中心跳转处理
    jumppersonal() {
      let hasuserlogin = localStorage.getItem("heima-headline-user");
      if (hasuserlogin) {
        // console.log(hasuserlogin);
        let id = JSON.parse(hasuserlogin).id;
        // console.log(id);
        this.$router.push({ path: "/mypersonal/" + id });
      } else {
        this.$router.push({ name: "login" });
      }
    },
  },
  components: {
    art,
  },
  data() {
    return {
      arr: [],
      active: localStorage.getItem("heima-headline-user") ? 1 : 0,
      gotops: false,
    };
  },
  async mounted() {
    //判断是否有本地存储,有则取出
    this.arr = JSON.parse(localStorage.getItem("heima-headline-cate-del"));
    //获取栏目分类
    if (!this.arr) {
      let res = await getlistname();
      this.arr = res.data.data;
    }
    // console.log(this.arr);
    //进行数据改造: 实现每一个栏目都有自己的新闻数据,且操作相互不干扰,当前栏目的数据结构不能满足现在的需求
    this.arr = this.arr.map((v) => {
      return {
        ...v,
        postlist: [], //添加当前栏目的数据形成的数组
        pageIndex: 1, //当前栏目的页码
        pageSize: 4, //当前栏目每页显示的数量
        finished: false,
        loading: false,
        isLoading: false,
      };
    });
    this.init();

    // 点击+ 区域跳转至栏目页面
    //因为该区域是伪元素,无法在页面结构中添加事件 ,使用js的方法进行添加数据
    document.querySelector(".van-sticky").onclick = (e) => {
      //方法1:  使用点击的类名来进行事件操作
      //获取该字节中的元素名称
      // let className = e.target.className;
      // console.log(className); //打印出被点击元素的类名
      //点击到伪元素是会打印出该元素的类名
      // if (className == "van-sticky") {
      // this.$router.push({ name: "mycolumn" });
      // }

      // 方法2:  使用给元素添加  pointer-events 属性实现点击事件
      this.$router.push({ name: "mycolumn" });
    };
  },
  watch: {
    async active() {
      //a:到达的index b:原来的index
      // postlists中没有数据时获取,有数据是不用再进行发送请求数据
      //postlist没有数据的时候再进行数据获取,有数据的时候就不进行重新获取数据,避免多次重复获取数据刷新页面
      if (this.arr[this.active].postlist.length == 0) {
        // console.log("切换栏目了");
        this.init();
      }
    },
  },
};
</script>

<style lang="less" scoped>
.tan {
  display: block;
  border-radius: 40%;
  text-align: center;
  background: rgba(43, 226, 125, 0.301);
  position: fixed;
  bottom: 140px;
  right: 140px;
  height: 50px;
  width: 50px;
  color: #000;
  font-size: 10px;
  z-index: 10;
}
.both {
  height: 45px;
}
.header {
  height: 45px;
  width: 100%;
  top: 0;
  position: fixed;
  display: flex;
  justify-content: space-between;
  background-color: #f00;
  align-items: center;
  color: #fff;
  z-index: 3;
}
/deep/.van-sticky {
  padding-right: 30px;
  pointer-events: none;
  &::after {
    content: "+";
    position: absolute;
    pointer-events: auto;
    top: 0;
    right: 0;
    width: 30px;
    height: 44px;
    background: rgba(223, 223, 223);
    font-size: 28px;
    text-align: center;
    line-height: 40px;
  }
}
.search {
  flex: 1;
  height: 30px;
  background-color: rgba(255, 255, 255, 0.4);
  border-radius: 15px;

  line-height: 30px;
  font-size: 14px;

  .van-icon {
    line-height: 30px;
    font-size: 16px;
  }
}
.van-icon {
  font-size: 24px;
  padding: 0 10px;
}
.iconfont {
  font-size: 50px;
  padding: 0 10px;
}
</style>